//
//  MyActivityViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/16.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MyActivityViewController.h"
#import "DataBaseSingleton.h"
#import "ActivityList.h"
#import "ActivityDetailViewController.h"
#import "AFNetworking.h"

@interface MyActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *allArray;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *actArray;

@end

@implementation MyActivityViewController

- (void)dealloc{
    [_myTableView release];
    [_allArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的活动";
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    self.allArray = [[DataBaseSingleton shareDataBase] selectActivity];
    NSLog(@"%ld", self.allArray.count);

    [self afnGetRequest];

    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:_myTableView];
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [_myTableView release];
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ActivityList *act = [self.allArray objectAtIndex:indexPath.row];
    cell.textLabel.text = act.title;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityDetailViewController *activityDetailVC = [[ActivityDetailViewController alloc] init];
    ActivityList *act = [self.actArray objectAtIndex:indexPath.row];
    activityDetailVC.activityList = act;
    
    [self.navigationController pushViewController:activityDetailVC animated:YES];
    [activityDetailVC release];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}



-(void)afnGetRequest
{
    
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

    self.actArray = [NSMutableArray array];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    //请求管理者 所有的网络请求,均有manager发起
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSLog(@"=====%d", [NSThread isMainThread]);
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            
        });
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        self.allArray = [NSMutableArray array];
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"events"];
        
        for (NSDictionary *dic in array) {
            ActivityList *activityList = [[ActivityList alloc] init];
            [activityList setValuesForKeysWithDictionary:dic];
            //            NSLog(@"%@", model);
            [self.actArray addObject:activityList];
            [activityList release];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    self.allArray = [NSMutableArray array];
    self.allArray = [[DataBaseSingleton shareDataBase] selectActivity];
    NSLog(@"%ld", self.allArray.count);
    ActivityList *act = [self.allArray objectAtIndex:indexPath.row];
    [[DataBaseSingleton shareDataBase] deleteActivityTable:act.title];
    NSLog(@"%@", act.title);
    NSLog(@"%ld", indexPath.row);
    [self.allArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
